<template>
  <div class="app-container">
    <yi-table
      :api-config="apis.getDepartmentTree"
      :right-tools="['refresh']"
      :columns="[
        {
          label: '部门名称',
          prop: 'name',
        },
        {
          label: '部门代码',
          prop: 'code',
        },
        {
          label: '操作',
          prop: 'opt',
          width: '360px',
          $slot: 'opt',
        },
      ]"
      default-expand-all
    >
      <template #search-form="{ model, refresh }">
        <el-form-item label="部门名称">
          <el-input
            v-model="model.name"
            placeholder="请输入部门名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门代码">
          <el-input
            v-model="model.code"
            placeholder="请输入部门代码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="mini" @click="refresh"
            >查询</el-button
          >
        </el-form-item>
      </template>

      <template #tool-bar="{ refresh, el }">
        <el-col :span="1.5">
          <!-- 添加 -->
          <yi-operation
            :api-config="apis.addDepartment"
            text="添加"
            type="primary"
            size="mini"
            :plain="true"
            icon="el-icon-plus"
            modal-title="添加部门"
            dialog-width="500px"
            @on-submit-success="refresh"
          >
            <template #default="scope">
              <dept-form v-model="scope.model" use-for="add" />
            </template>
          </yi-operation>
        </el-col>

        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="el.toggleAllExpansion()"
            >展开/折叠</el-button
          >
        </el-col>
      </template>

      <template #opt="{ row }">
        <yi-operation
          :api-config="apis.updateDepartment"
          type="text"
          text="修改"
          size="mini"
          icon="el-icon-edit"
          :form-model="row"
          modal-title="修改部门"
          dialog-width="500px"
          @on-submit-success="
            (formModel) => {
              Object.assign(row, formModel);
            }
          "
        >
          <template #default="scope">
            <dept-form v-model="scope.model" use-for="edit" />
          </template>
        </yi-operation>
      </template>
    </yi-table>
  </div>
</template>

<script>
import DeptForm from "./dept-form.vue";
import {
  getDepartmentTree,
  addDepartment,
  updateDepartment,
} from "@/apis/system/dept";
export default {
  name: "DeptPage",
  apis: {
    getDepartmentTree,
    addDepartment,
    updateDepartment,
  },
  pageInfo: {
    title: "部门管理",
    permission: 'system:department:view'
  },
  components: { DeptForm },
  data() {
    return {};
  },
  methods: {},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
